<?php
namespace SimplePhp\Dispatcher;
use SimplePhp\Map as map;
use SimplePhp\System as system;

/**
 * 扫描Controller目录下面的类文件
 * 二维数组
 * [控制器名称]=>
 * 	[0]=>控制器Node反射实例
 * 	[方法名]=>方法反射实例
 */
class ControllerNodeScanner {
	public $dispatchMap = [];
	/**
	 * 扫描Controller文件
	 * Controller文件命名规则
	 * 类名.命名空间.php
	 */
	public function Scanner() {
		$file = new system\File();
		if (!defined("CONTROLLER_PATH")) {
			return;
		}
		//缓存
		$listOffile = $file->RecursiveFile(CONTROLLER_PATH);
		if (!empty($listOffile)) {
			foreach ($listOffile as $key => $value) {
				if (is_array($value)) {
					//Controller同名文件会取先找到的文件
					$value = $value[0];
				}
				$expload = explode('.', $key);
				$classname = $expload[1] . '\\' . $expload[0];
				$controller = new map\NodeReflectionClass($classname);
				$controllerNode = $controller->getNode(new map\Controller());
				$pageMethod = $controller->getMethods();

				//携带Class的反射  mathon数组第一个是controller
				$this->dispatchMap[$controllerNode->name][] = $controller;

				foreach ($pageMethod as $key => $value) {
					$Response = $value->getNode(new map\Response());
					$this->dispatchMap[$controllerNode->name][$Response->name] = $value;
				}
			}
		}
		// var_dump($this->dispatchMap);
		return $this->dispatchMap;
	}

}